<?php
namespace app\api\controller;

use app\admin\model\system\System;
use app\admin\model\teamrecord\Teamrecord;
use app\common\controller\Api;
use think\Db;
use think\Exception;

class Team extends Api{
    protected $noNeedLogin = ['tepiteam'];
    protected $noNeedRight = ['*'];

    public function index(){
        $userinfo = $this->auth->getUserinfo();
        $userModel = new \app\admin\model\User();
        $allinvitenums = $userModel->where("FIND_IN_SET({$userinfo['id']}, pids)")->count();
        $_time=strtotime(date('Ymd',time()));
        $where['createtime']=['gt',$_time];
        $todayinvitenums = $userModel->where("FIND_IN_SET({$userinfo['id']}, pids)")->where($where)->count();

        //我的直推总人数
        $myzhituinums = $userModel->where(['pid'=>$userinfo['id']])->count();

        //我的团队总人数
        $myteamnums = $userModel->where("FIND_IN_SET({$userinfo['id']}, pids)")->count();

        $this->success('成功',[
            'allinvitenums'=>$allinvitenums,
            'todayinvitenums'=>$todayinvitenums,
            'myzhituinums'=>$myzhituinums,
            'myteamnums'=>$myteamnums]);
    }

    public function invitelist(){
        $page = $this->request->param('page');
        if(!$page){
            $page = 1;
        }
        $pageSize = 10;
        $start = ($page-1)*$pageSize;
        $type = $this->request->param('type');
        $keyword = $this->request->param('keyword');
        $swhere=[];
        if($keyword!=''){
            $swhere['nickanme'] = ['like','%'.$keyword.'%'];
        }
        $userinfo = $this->auth->getUserinfo();
        $userModel = new \app\admin\model\User();
        $rechargeModel = new \app\admin\model\recharge\Recharge();

        $where['pid'] = $userinfo['id'];

        if($type == 1){
            $swhere['is_auth'] = 1;
        } else if($type == 2){
            $swhere['is_auth'] = 0;
        }

        // 使用JOIN联表查询
        $list = collection($userModel->where($where)->where($swhere)->limit($start,$pageSize)->select());

        if($list){
            $list = $list->toArray();
            $newlist = [];
            foreach($list as $k => $v){
                $newlist[$k]['id'] = $v['id'];
                $newlist[$k]['nickname'] = $v['nickname'];
                if($v['avatar']==''){
                    $newlist[$k]['avatar'] = 'http://47.239.156.92/avatar.png';
                }else{
                    $newlist[$k]['avatar'] = $v['avatar'];
                }
                $newlist[$k]['level'] = $v['level'];
                $newlist[$k]['invite_nickname'] = $v['nickname'];
                $newlist[$k]['amount'] = $rechargeModel->where(['user_id'=>$v['id'],'status'=>1])->sum('amount');
                if($v['identity_id']==1){
                    $newlist[$k]['identity_name'] = '创客';
                }else if($v['identity_id']==2){
                    $newlist[$k]['identity_name'] = '区长';
                }else if($v['identity_id']==3){
                    $newlist[$k]['identity_name'] = '城主';
                }else if($v['identity_id']==4 || $v['identity_id']==0){
                    $newlist[$k]['identity_name'] = '游客';
                }else if($v['identity_id']==5){
                    $newlist[$k]['identity_name'] = '市商';
                }
            }
            $this->success('成功', $newlist);
        } else {
            $this->success('成功', []);
        }
    }

    public function myup(){
        $userinfo = $this->auth->getUserinfo();
        $userModel = new \app\admin\model\User();
        $info = $userModel->where(['id' => $userinfo['pid']])->find();
        $pinfo = $userModel->where(['id'=>$info['pid']])->find();
        $rechargeModel = new \app\admin\model\recharge\Recharge();
        $rechargeinfo = $rechargeModel->where(['user_id'=>$pinfo['id'], 'status'=>1])->find();
        if($rechargeinfo){
            $amount = $rechargeinfo['amount'];
        }else{
            $amount = 0;
        }
        if($pinfo){
            $pinfo =$pinfo->toArray();
            $p_nickname = $pinfo['nickname'];
        }else{
            $p_nickname = '无';
        }
        if($info){
            $info = $info->toArray();
            if($info['avatar']==''){
                $avatar = 'http://47.239.156.92/avatar.png';
            }else{
                $avatar = $info['avatar'];
            }
            $backData=[
                'avatar'=>$avatar,
                'level'=>$info['level'],
                'id'=>$info['id'],
                'nickname'=>$info['nickname'],
                'intite_nickname'=>$p_nickname,
                'amount'=>$amount,
                'province'=>$info['province'],
                'city'=>$info['city'],
                'district'=>$info['district'],
            ];
            $this->success('成功',$backData);
        }else{
            $this->success('成功',[]);
        }
    }

    /*public function alllist(){
        $page = $this->request->param('page');
        if(!$page){
            $page = 1;
        }
        $pageSize = 10;
        $start = ($page-1)*$pageSize;
        $userinfo = $this->auth->getUserinfo();
        $userModel = new \app\admin\model\User();
        $rechargeModel =new \app\admin\model\recharge\Recharge();

        $list = collection($userModel
            ->where("FIND_IN_SET('{$userinfo['id']}', pids)")
            ->limit($start, $pageSize)
            ->select());
        if($list){
            $list = $list->toArray();
            $newlist = [];
            foreach($list as $k => $v){
                $newlist[$k]['id'] = $v['id'];
                $newlist[$k]['nickname'] = $v['nickname'];
                $newlist[$k]['level'] = $v['level'];
                $newlist[$k]['username'] = $v['username'];
                $newlist[$k]['usdt_amount'] = $rechargeModel->where(['user_id'=>$v['id'],'unit'=>2,'status'=>1])->sum('amount');
                $newlist[$k]['hc_amount'] = $rechargeModel->where(['user_id'=>$v['id'],'unit'=>1,'status'=>1])->sum('amount');
                $newlist[$k]['team_nums'] = $userModel->where("FIND_IN_SET('{$v['id']}', pids)")->count();
                if($v['avatar']==''){
                    $newlist[$k]['avatar'] = 'http://47.239.156.92/avatar.png';
                }else{
                    $newlist[$k]['avatar'] = $v['avatar'];
                }
                $newlist[$k]['level'] = $v['level'];
                $pinfo = $userModel->where(['id'=>$v['pid']])->find()->toArray();
                $newlist[$k]['invite_nickname'] = $pinfo['nickname'];
                $newlist[$k]['amount'] = $rechargeModel->where(['user_id'=>$v['id'],'status'=>1])->sum('amount');
                if($v['identity_id']==1){
                    $newlist[$k]['identity_name'] = '创客';
                }else if($v['identity_id']==2){
                    $newlist[$k]['identity_name'] = '区长';
                }else if($v['identity_id']==3){
                    $newlist[$k]['identity_name'] = '城主';
                }else if($v['identity_id']==4 || $v['identity_id']==0){
                    $newlist[$k]['identity_name'] = '游客';
                }
                $newlist[$k]['is_shishang'] = $v['is_shishang'];
                $newlist[$k]['register_time'] = date("Y-m-d H:i:s",$v['createtime']);
            }
            $this->success('成功', $newlist);
        }else{
            $this->success('成功', []);
        }
    }*/
    public function alllist(){
        $page = $this->request->param('page', 1);
        $pageSize = 10;
        $start = ($page - 1) * $pageSize;
        $userinfo = $this->auth->getUserinfo();
        $userModel = new \app\admin\model\User();
        $rechargeModel = new \app\admin\model\recharge\Recharge();

        // 获取用户列表
        $list = collection($userModel
            ->where("FIND_IN_SET('{$userinfo['id']}', pids)")
            ->limit($start, $pageSize)
            ->select());

        if(!$list){
            $this->success('成功', []);
        }

        $list = $list->toArray();
        $userIds = array_column($list, 'id');
        $pids = array_column($list, 'pid');

        // 批量查询上级用户信息
        $parentUsers = $userModel
            ->where('id', 'in', $pids)
            ->column('nickname', 'id');

        // 批量查询充值统计（USDT）
        $usdtRecharges = $rechargeModel
            ->where('user_id', 'in', $userIds)
            ->where(['unit' => 2, 'status' => 1])
            ->group('user_id')
            ->field('user_id, SUM(amount) as usdt_amount')
            ->select();
        $usdtAmountMap = [];
        foreach($usdtRecharges as $item){
            $usdtAmountMap[$item['user_id']] = $item['usdt_amount'];
        }

        // 批量查询充值统计（HC）
        $hcRecharges = $rechargeModel
            ->where('user_id', 'in', $userIds)
            ->where(['unit' => 1, 'status' => 1])
            ->group('user_id')
            ->field('user_id, SUM(amount) as hc_amount')
            ->select();
        $hcAmountMap = [];
        foreach($hcRecharges as $item){
            $hcAmountMap[$item['user_id']] = $item['hc_amount'];
        }

        // 批量查询总充值统计
        $totalRecharges = $rechargeModel
            ->where('user_id', 'in', $userIds)
            ->where(['status' => 1])
            ->group('user_id')
            ->field('user_id, SUM(amount) as total_amount')
            ->select();
        $totalAmountMap = [];
        foreach($totalRecharges as $item){
            $totalAmountMap[$item['user_id']] = $item['total_amount'];
        }

        // 批量查询团队人数
        $teamCounts = [];
        foreach($userIds as $userId){
            $count = $userModel->where("FIND_IN_SET('{$userId}', pids)")->count();
            $teamCounts[$userId] = $count;
        }

        // 构建返回数据
        $newlist = [];
        foreach($list as $k => $v){
            $newlist[$k]['id'] = $v['id'];
            $newlist[$k]['nickname'] = $v['nickname'];
            $newlist[$k]['level'] = $v['level'];
            $newlist[$k]['username'] = $v['username'];
            $newlist[$k]['usdt_amount'] = $usdtAmountMap[$v['id']] ?? 0;
            $newlist[$k]['hc_amount'] = $hcAmountMap[$v['id']] ?? 0;
            $newlist[$k]['team_nums'] = $teamCounts[$v['id']] ?? 0;
            $newlist[$k]['avatar'] = $v['avatar'] ?: 'http://47.239.156.92/avatar.png';
            $newlist[$k]['level'] = $v['level'];

            // 身份映射
            $identityMap = [
                1 => '创客',
                2 => '区长',
                3 => '城主',
                0 => '游客',
                4 => '游客'
            ];
            $newlist[$k]['identity_name'] = $identityMap[$v['identity_id']] ?? '游客';
            $newlist[$k]['register_time'] = date("Y-m-d H:i:s", $v['createtime']);
        }

        $this->success('成功', $newlist);
    }

    public function tepiteam(){
        $systemModel = new System();
        $userModel = new \app\admin\model\User();
        $teamrecordModel = new Teamrecord();
        $levelModel = new \app\admin\model\level\Level();
        $systeminfo = $systemModel->where(['id'=>1])->find()->toArray();
        if($systeminfo['tepi_team_id'] != ''){
            $idarr = explode(',', $systeminfo['tepi_team_id']);
            Db::startTrans();
            try{
                foreach ($idarr as $k=>$v){
                    $userinfo = $userModel->where(['id' => $v])->find()->toArray();
                    if($userinfo['level'] ==1){
                        $levelinfo = $levelModel->where(['id'=>1])->find()->toArray();
                        if($userinfo['team_amount']>0){
                            $one = $userinfo['team_amount']/365*$levelinfo['bili']/100;
                            //修改用户收益和团队奖励
                            $userModel->where(['id'=>$userinfo['id']])->update([
                                'shouyi_balance'=>$userinfo['shouyi_balance']+$one,
                                'tuandui_amount'=>$userinfo['tuandui_amount']+$one,
                                'today_team_shouyi'=>$one
                            ]);

                            //写入团队奖纪录
                            $indata['user_id'] = $userinfo['id'];
                            $indata['team_amount'] = $userinfo['team_amount']/365;
                            $indata['amount'] = $one;
                            $indata['bili']=$levelinfo['bili'];
                            $indata['event']='用户'.$userinfo['nickname'].'等级达到战士获取团队奖励'.$one;
                            $indata['create_time'] = time();
                            $teamrecordModel->insert($indata);
                        }
                    }else if($userinfo['level'] ==2){
                        $levelinfo = $levelModel->where(['id'=>2])->find()->toArray();
                        if($userinfo['team_amount']>0){
                            $one = $userinfo['team_amount']/365*$levelinfo['bili']/100;
                            //修改用户收益和团队奖励
                            $userModel->where(['id'=>$userinfo['id']])->update([
                                'shouyi_balance'=>$userinfo['shouyi_balance']+$one,
                                'tuandui_amount'=>$userinfo['tuandui_amount']+$one,
                                'today_team_shouyi'=>$one
                            ]);

                            //写入团队奖纪录
                            $indata['user_id'] = $userinfo['id'];
                            $indata['team_amount'] = $userinfo['team_amount']/365;
                            $indata['amount'] = $one;
                            $indata['bili']=$levelinfo['bili'];
                            $indata['event']='用户'.$userinfo['nickname'].'等级达到战士获取团队奖励'.$one;
                            $indata['create_time'] = time();
                            $teamrecordModel->insert($indata);
                        }
                    }else if($userinfo['level'] ==3){
                        if($userinfo['team_amount']>0){
                            $one = $userinfo['team_amount']/365*0.02;
                            //修改用户收益和团队奖励
                            $userModel->where(['id'=>$userinfo['id']])->update([
                                'shouyi_balance'=>$userinfo['shouyi_balance']+$one,
                                'tuandui_amount'=>$userinfo['tuandui_amount']+$one,
                                'today_team_shouyi'=>$one
                            ]);

                            //写入团队奖纪录
                            $indata['user_id'] = $userinfo['id'];
                            $indata['team_amount'] = $userinfo['team_amount']/365;
                            $indata['amount'] = $one;
                            $indata['bili']=2;
                            $indata['event']='用户'.$userinfo['nickname'].'等级达到战士获取团队奖励'.$one;
                            $indata['create_time'] = time();
                            $teamrecordModel->insert($indata);

                            $info = $userModel->where(['id'=>$userinfo['id']])->find()->toArray();
                            $level = $levelModel->where(['id'=>$userinfo['level']])->find()->toArray();
                            if($info['pids']!=''){
                                $idarr=explode(',',$info['pids']);
                                $idarr=array_reverse($idarr);
                                foreach ($idarr as $k1=>$v1){
                                    $pinfo = $userModel->where(['id'=>$v1])->find();
                                    if($pinfo){
                                        $pinfo = $pinfo->toArray();
                                        if($pinfo['level']>0){
                                            $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                            if($pinfo['level']<3){

                                            }else{
                                                if($pinfo['level']<=$info['level']){
                                                    $pamount = $info['today_shouyi']*0.1;
                                                    if($pamount>0){
                                                        //上级用户增加余额
                                                        $userModel->where(['id'=>$pinfo['id']])->update([
                                                            'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                            'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                        ]);
                                                        //写团队奖纪录
                                                        $indata2['user_id'] = $pinfo['id'];
                                                        $indata2['team_amount'] = $info['today_shouyi'];
                                                        $indata2['amount'] = $pamount;
                                                        $indata2['bili'] = 10;
                                                        $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                                        $indata2['create_time'] = time();
                                                        $teamrecordModel->insert($indata2);
                                                    }
                                                }else{
                                                    //计算极差
                                                    $cha = $plevelinfo['bili']-$level['bili'];
                                                    $pamount = $userinfo['team_amount']/365*$cha/100;
                                                    if($pamount>0){
                                                        //上级用户增加余额
                                                        $userModel->where(['id'=>$pinfo['id']])->update([
                                                            'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                            'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                        ]);
                                                        //写团队奖纪录
                                                        $indata2['user_id'] = $pinfo['id'];
                                                        $indata2['team_amount'] = $userinfo['team_amount']/365;
                                                        $indata2['amount'] = $pamount;
                                                        $indata2['bili'] = $cha;
                                                        $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队当日充值额的'.$cha.'%为'.$pamount;
                                                        $indata2['create_time'] = time();
                                                        $teamrecordModel->insert($indata2);
                                                    }
                                                }
                                            }
                                            $info = $pinfo;
                                            $level = $plevelinfo;
                                        }
                                    }
                                }
                            }
                        }
                    }else if($userinfo['level'] ==4){
                        if($userinfo['team_amount']>0){
                            $one = $userinfo['team_amount']/365*0.02;
                            //修改用户收益和团队奖励
                            $userModel->where(['id'=>$userinfo['id']])->update([
                                'shouyi_balance'=>$userinfo['shouyi_balance']+$one,
                                'tuandui_amount'=>$userinfo['tuandui_amount']+$one,
                                'today_team_shouyi'=>$one
                            ]);

                            //写入团队奖纪录
                            $indata['user_id'] = $userinfo['id'];
                            $indata['team_amount'] = $userinfo['team_amount']/365;
                            $indata['amount'] = $one;
                            $indata['bili']=2;
                            $indata['event']='用户'.$userinfo['nickname'].'等级达到战士获取团队奖励'.$one;
                            $indata['create_time'] = time();
                            $teamrecordModel->insert($indata);

                            $info = $userModel->where(['id'=>$userinfo['id']])->find()->toArray();
                            $level = $levelModel->where(['id'=>$userinfo['level']])->find()->toArray();
                            if($info['pids']!=''){
                                $idarr=explode(',',$info['pids']);
                                $idarr=array_reverse($idarr);
                                foreach ($idarr as $k1=>$v1){
                                    $pinfo = $userModel->where(['id'=>$v1])->find();
                                    if($pinfo){
                                        $pinfo = $pinfo->toArray();
                                        if($pinfo['level']>0){
                                            $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                            if($pinfo['level']<3){

                                            }else{
                                                if($pinfo['level']<=$info['level']){
                                                    $pamount = $info['today_shouyi']*0.1;
                                                    if($pamount>0){
                                                        //上级用户增加余额
                                                        $userModel->where(['id'=>$pinfo['id']])->update([
                                                            'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                            'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                        ]);
                                                        //写团队奖纪录
                                                        $indata2['user_id'] = $pinfo['id'];
                                                        $indata2['team_amount'] = $info['today_shouyi'];
                                                        $indata2['amount'] = $pamount;
                                                        $indata2['bili'] = 10;
                                                        $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                                        $indata2['create_time'] = time();
                                                        $teamrecordModel->insert($indata2);
                                                    }
                                                }else{
                                                    //计算极差
                                                    $cha = $plevelinfo['bili']-$level['bili'];
                                                    $pamount = $userinfo['team_amount']/365*$cha/100;
                                                    if($pamount>0){
                                                        //上级用户增加余额
                                                        $userModel->where(['id'=>$pinfo['id']])->update([
                                                            'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                            'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                        ]);
                                                        //写团队奖纪录
                                                        $indata2['user_id'] = $pinfo['id'];
                                                        $indata2['team_amount'] = $userinfo['team_amount']/365;
                                                        $indata2['amount'] = $pamount;
                                                        $indata2['bili'] = $cha;
                                                        $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队当日充值额的'.$cha.'%为'.$pamount;
                                                        $indata2['create_time'] = time();
                                                        $teamrecordModel->insert($indata2);
                                                    }
                                                }
                                            }
                                            $info = $pinfo;
                                            $level = $plevelinfo;
                                        }
                                    }
                                }
                            }
                        }
                    }else if($userinfo['level'] ==5){
                        if($userinfo['team_amount']>0){
                            $one = $userinfo['team_amount']/365*0.02;
                            //修改用户收益和团队奖励
                            $userModel->where(['id'=>$userinfo['id']])->update([
                                'shouyi_balance'=>$userinfo['shouyi_balance']+$one,
                                'tuandui_amount'=>$userinfo['tuandui_amount']+$one,
                                'today_team_shouyi'=>$one
                            ]);

                            //写入团队奖纪录
                            $indata['user_id'] = $userinfo['id'];
                            $indata['team_amount'] = $userinfo['team_amount']/365;
                            $indata['amount'] = $one;
                            $indata['bili']=2;
                            $indata['event']='用户'.$userinfo['nickname'].'等级达到战士获取团队奖励'.$one;
                            $indata['create_time'] = time();
                            $teamrecordModel->insert($indata);

                            $info = $userModel->where(['id'=>$userinfo['id']])->find()->toArray();
                            $level = $levelModel->where(['id'=>$userinfo['level']])->find()->toArray();
                            if($info['pids']!=''){
                                $idarr=explode(',',$info['pids']);
                                $idarr=array_reverse($idarr);
                                foreach ($idarr as $k1=>$v1){
                                    $pinfo = $userModel->where(['id'=>$v1])->find();
                                    if($pinfo){
                                        $pinfo = $pinfo->toArray();
                                        if($pinfo['level']>0){
                                            $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                            if($pinfo['level']<3){

                                            }else{
                                                if($pinfo['level']<=$info['level']){
                                                    $pamount = $info['today_shouyi']*0.1;
                                                    if($pamount>0){
                                                        //上级用户增加余额
                                                        $userModel->where(['id'=>$pinfo['id']])->update([
                                                            'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                            'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                        ]);
                                                        //写团队奖纪录
                                                        $indata2['user_id'] = $pinfo['id'];
                                                        $indata2['team_amount'] = $info['today_shouyi'];
                                                        $indata2['amount'] = $pamount;
                                                        $indata2['bili'] = 10;
                                                        $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                                        $indata2['create_time'] = time();
                                                        $teamrecordModel->insert($indata2);
                                                    }
                                                }else{
                                                    //计算极差
                                                    $cha = $plevelinfo['bili']-$level['bili'];
                                                    $pamount = $userinfo['team_amount']/365*$cha/100;
                                                    if($pamount>0){
                                                        //上级用户增加余额
                                                        $userModel->where(['id'=>$pinfo['id']])->update([
                                                            'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                            'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                        ]);
                                                        //写团队奖纪录
                                                        $indata2['user_id'] = $pinfo['id'];
                                                        $indata2['team_amount'] = $userinfo['team_amount']/365;
                                                        $indata2['amount'] = $pamount;
                                                        $indata2['bili'] = $cha;
                                                        $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队当日充值额的'.$cha.'%为'.$pamount;
                                                        $indata2['create_time'] = time();
                                                        $teamrecordModel->insert($indata2);
                                                    }
                                                }
                                            }
                                            $info = $pinfo;
                                            $level = $plevelinfo;
                                        }
                                    }
                                }
                            }
                        }
                    }else if($userinfo['level'] ==6){
                        if($userinfo['team_amount']>0){
                            $one = $v['team_amount']/365*0.03;
                            //修改用户收益和团队奖励
                            $userModel->where(['id'=>$userinfo['id']])->update([
                                'shouyi_balance'=>$userinfo['shouyi_balance']+$one,
                                'tuandui_amount'=>$userinfo['tuandui_amount']+$one,
                                'today_team_shouyi'=>$one
                            ]);

                            //写入团队奖纪录
                            $indata['user_id'] = $userinfo['id'];
                            $indata['team_amount'] = $userinfo['team_amount']/365;
                            $indata['amount'] = $one;
                            $indata['bili']=3;
                            $indata['event']='用户'.$userinfo['nickname'].'等级达到战士获取团队奖励'.$one;
                            $indata['create_time'] = time();
                            $teamrecordModel->insert($indata);

                            $info = $userModel->where(['id'=>$userinfo['id']])->find()->toArray();
                            $level = $levelModel->where(['id'=>$userinfo['level']])->find()->toArray();
                            if($info['pids']!=''){
                                $idarr=explode(',',$info['pids']);
                                $idarr=array_reverse($idarr);
                                foreach ($idarr as $k1=>$v1){
                                    $pinfo = $userModel->where(['id'=>$v1])->find();
                                    if($pinfo){
                                        $pinfo = $pinfo->toArray();
                                        if($pinfo['level']>0){
                                            $plevelinfo =  $levelModel->where(['id'=>$pinfo['level']])->find()->toArray();
                                            if($pinfo['level']<3){

                                            }else{
                                                if($pinfo['level']<=$info['level']){
                                                    $pamount = $info['today_shouyi']*0.1;
                                                    if($pamount>0){
                                                        //上级用户增加余额
                                                        $userModel->where(['id'=>$pinfo['id']])->update([
                                                            'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                            'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                        ]);
                                                        //写团队奖纪录
                                                        $indata2['user_id'] = $pinfo['id'];
                                                        $indata2['team_amount'] = $info['today_shouyi'];
                                                        $indata2['amount'] = $pamount;
                                                        $indata2['bili'] = 10;
                                                        $indata2['event'] = '用户'.$pinfo['nickname'].'不满足极差条件拿下家用户'.$info['nickname'].'日收益的10%为'.$pamount;
                                                        $indata2['create_time'] = time();
                                                        $teamrecordModel->insert($indata2);
                                                    }
                                                }else{
                                                    //计算极差
                                                    $cha = $plevelinfo['bili']-$level['bili'];
                                                    $pamount = $userinfo['team_amount']/365*$cha/100;
                                                    if($pamount>0){
                                                        //上级用户增加余额
                                                        $userModel->where(['id'=>$pinfo['id']])->update([
                                                            'shouyi_balance'=>$pinfo['shouyi_balance']+$pamount,
                                                            'tuandui_amount'=>$pinfo['tuandui_amount']+$pamount,
                                                        ]);
                                                        //写团队奖纪录
                                                        $indata2['user_id'] = $pinfo['id'];
                                                        $indata2['team_amount'] = $userinfo['team_amount']/365;
                                                        $indata2['amount'] = $pamount;
                                                        $indata2['bili'] = $cha;
                                                        $indata2['event'] = '用户'.$pinfo['nickname'].'满足极差条件拿用户'.$info['nickname'].'团队当日充值额的'.$cha.'%为'.$pamount;
                                                        $indata2['create_time'] = time();
                                                        $teamrecordModel->insert($indata2);
                                                    }
                                                }
                                            }
                                            $info = $pinfo;
                                            $level = $plevelinfo;
                                        }
                                    }
                                }
                            }
                        }
                    }else{

                    }
                }
                Db::commit();
                $this->success('发送特批团队奖成功');
            }catch(Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
            foreach ($idarr as $k => $v) {

            }
        }else{
            $this->success('没有修改团队特批');
        }
    }
}
